#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>
#include <QString>
#include <QStringList>
#include <QMap>
#include <QPoint>
#include <QMouseEvent>

class QListWidgetItem;
class ListWidgetItem;
class ChatWidget;
class MessageSocket;
class LikeDatabase;
class GlobalConfig;
class QTcpServer;

namespace Ui {
class MainWidget;
}

class MainWidget : public QWidget
{
    Q_OBJECT
//重命名两种迭代器
typedef QMap<QString, ListWidgetItem*>::iterator ListWidgetItemMapIterator;
typedef QMap<QString, ChatWidget*>::iterator ChatWidgetMapIterator;

public:
    //音效类型
    enum SoundType{SOUND_ONLINE = 0, SOUND_MESSAGE, SOUND_NEWFRIEND};
public:
    explicit MainWidget(QWidget *parent = 0);
    ~MainWidget();

private:

protected:
    //在关闭事件中清理所有对话窗口
    void closeEvent(QCloseEvent *event);
    //以下三个事件处理函数实现界面拖动效果
    //鼠标按下事件
    void mousePressEvent(QMouseEvent *event);
    //鼠标释放事件
    void mouseReleaseEvent(QMouseEvent *event);
    //鼠标移动事件
    void mouseMoveEvent(QMouseEvent *event);

private slots:
    //发送消息
    void sendMessage(const QString &uuid, const QString &message);
    //发送图片
    void sendImage(const QString &uuid, const QString &imageName);
    //发送文件
    void sendFile(const QString &uuid, const QString &fileName);
    //有人发来文件
    void onNewConnection();
    //当有item被双击选中
    void onItemDoubleClicked(QListWidgetItem *item);
    //uuid对应的对话框被关掉
    void thisUUidChatWidgetClosed(const QString &uuid);
    //切换皮肤
    void changeStyleSheet();
    //切换页面槽函数
    void on_btnFriend_clicked();
    void on_btnGroup_clicked();
    void on_btnSet_clicked();

private:
    //把好友添加进朋友列表中
    void addFriend(const QString &friendUuid, const QString &friendName);
    //播放音效
    void playSound(SoundType type);

private slots:
    //属于消息接受的槽函数
    void recvLoginMessage(const QString &friendUuid, const QString &friendName);
    void recvEchoLoginMessage(const QString &friendUuid, const QString &friendName);
    void recvTextMessage(const QString &friendUuid, const QString &text);
    void recvNameMessage(const QString &friendUuid, const QString &friendName);
    void recvPhotoMessage(const QString &friendUuid, const QPixmap &friendPhoto);
    void recvImageMessage(const QString &friendUuid, const QImage &image);
    void recvExitMessage(const QString &friendUuid);
    //属于friendListWidget的右键菜单Action按钮槽函数
    void on_actionDeleteFriend_triggered();
    void on_actionDeleteChatRecord_triggered();
    void on_actionSetName_triggered();
    //属于头像，个性签名，头像，右键修改设置槽函数
    void on_labName_customContextMenuRequested(const QPoint &pos);
    void on_labAutograph_customContextMenuRequested(const QPoint &pos);
    void on_labPhoto_customContextMenuRequested(const QPoint &pos);
    //属于设置通信端口和下载文件目录的槽函数
    void on_btnDownloadPath_clicked();
    void on_btnTcpPort_clicked();
    void on_btnBroadcastPort_clicked();
    void on_btnUdpPort_clicked();

private:
    Ui::MainWidget *ui;
    //配置数据
    GlobalConfig *m_globalConfig;
    //用于局域网通信
    MessageSocket *m_messageSocket;
    //用于接收文件
    QTcpServer *m_server;
    //存储数据
    LikeDatabase *m_database;
    //用来存储鼠标指针与窗口位置的差值
    QPoint m_offset;
    //key都是uuid
    QMap<QString, ListWidgetItem*> m_listWidgetItemMap;
    QMap<QString, ChatWidget*> m_chatWidgetMap;
};

#endif // MAINWIDGET_H
